<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="地址名称" prop="hex">
                <el-input
                    v-model="queryParams.addressName"
                    placeholder="请输入地址名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="通道" prop="channelNum">
                <el-select v-model="queryParams.channelNum"
                           placeholder="通道号"
                           style="width: 100%"
                           clearable
                >
                    <el-option
                        v-for="item in channelNumList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"

                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"

                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"

                >删除
                </el-button>
            </el-col>

            <el-col :span="1.5">
                <el-button
                    type="info"
                    plain
                    icon="el-icon-upload2"
                    size="mini"
                    @click="handleImport"

                >导入
                </el-button>
            </el-col>

            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    :disabled="multiple"
                    @click="batchRead"
                >批量读取
                </el-button>
            </el-col>

            <!--            <el-col :span="1.5">-->
            <!--                <el-button-->
            <!--                    type="warning"-->
            <!--                    plain-->
            <!--                    icon="el-icon-download"-->
            <!--                    size="mini"-->
            <!--                    @click="handleExport"-->
            <!--                    v-hasPermi="['module:fertilizerDistributorV1Address:export']"-->
            <!--                >导出-->
            <!--                </el-button>-->
            <!--            </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading"
                  ref="addressTable"
                  :data="fertilizerDistributorV1AddressList"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="地址名称" align="center" prop="addressName"/>
            <el-table-column label="十六进制地址" align="center" prop="hex"/>
            <el-table-column label="十进制地址" align="center" prop="decimalDigit"/>
            <el-table-column label="位个数" align="center" prop="bitSize"/>
            <el-table-column label="寄存器个数" align="center" prop="registerSize"/>
            <!--            <el-table-column label="期望数值类型" align="center" prop="valueType"/>-->
            <el-table-column label="属性" align="center" prop="attribute">
                <template v-slot="scope">
                    <div v-show="scope.row.attribute==='0'">
                        <el-tag type="success">可读</el-tag>
                    </div>
                    <div v-show="scope.row.attribute==='1'">
                        <el-tag type="success">可写</el-tag>
                    </div>
                    <div v-show="scope.row.attribute==='0,1'">
                        <el-tag type="success">可读</el-tag>
                        <el-tag type="success" style="margin-left: 5px">可写</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="符号" align="center" prop="isSigned">
                <template v-slot="scope">
                    <span v-if="scope.row.isSigned==='0'">无符号</span>
                    <span v-if="scope.row.isSigned==='1'">有符号</span>
                </template>
            </el-table-column>
            <el-table-column label="缩放倍数" align="center" prop="weight"/>
            <el-table-column label="单位" align="center" prop="unit"/>
            <el-table-column label="当前值" align="center" prop="addressValue">
                <template v-slot="scope">
                    <el-input v-model="scope.row.addressValue" v-if="scope.row.attribute==='1'"/>
                    <el-input v-model="scope.row.addressValue" v-if="scope.row.attribute==='0,1'"/>
                    <span v-if="scope.row.attribute==='0'">{{ scope.row.addressValue }}</span>
                </template>
            </el-table-column>
            <el-table-column label="通道号" align="center" prop="channelNum"/>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleUpdate(scope.row)"

                    >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        v-if="scope.row.attribute==='0,1' || scope.row.attribute==='0'"
                        :loading="scope.row.readLoading"
                        @click="handleOperation(scope.row, scope.$index,'0')"
                    >{{ scope.row.readText }}
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        v-if="scope.row.attribute==='0,1' || scope.row.attribute==='1'"
                        :loading="scope.row.writeLoading"
                        @click="handleOperation(scope.row, scope.$index,'1')"
                    >{{ scope.row.writeText }}
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleDelete(scope.row)"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改施肥机v1地址对话框 -->
        <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-form-item label="地址名称" prop="addressName">
                    <el-input v-model="form.addressName" placeholder="请输入地址名称"/>
                </el-form-item>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="十六进制" prop="hex">
                            <el-input v-model="form.hex" placeholder="请输入十六进制地址"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="十进制" prop="decimalDigit">
                            <el-input v-model="form.decimalDigit" placeholder="请输入十进制地址"/>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="位个数" prop="bitSize">
                            <el-input v-model="form.bitSize" placeholder="请输入位个数"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="寄存器个数" prop="registerSize">
                            <el-input v-model="form.registerSize"
                                      placeholder="请输入寄存器个数"
                                      type="number"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="属性">
                            <el-checkbox-group v-model="form.attributeArr">
                                <el-checkbox label="0">可读</el-checkbox>
                                <el-checkbox label="1">可写</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="符号" prop="isSigned">
                            <el-radio-group v-model="form.isSigned">
                                <el-radio label="0">无符号</el-radio>
                                <el-radio label="1">有符号</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="缩放倍数" prop="weight">
                            <el-select v-model="form.weight"
                                       placeholder="缩放倍数"
                            >
                                <el-option
                                    v-for="item in dict.type.biz_weight"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="数值类型" prop="valueType">
                            <el-select v-model="form.valueType"
                                       placeholder="数值类型"
                            >
                                <el-option
                                    v-for="item in dict.type.biz_value_type"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="地址类型" prop="addressType">
                            <el-select v-model="form.addressType"
                                       placeholder="地址类型" style="width: 100%"
                            >
                                <el-option
                                    v-for="item in addressTypeList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="通道" prop="channelNum">
                            <el-select v-model="form.channelNum"
                                       placeholder="通道号" style="width: 100%"
                            >
                                <el-option
                                    v-for="item in channelNumList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="单位" prop="unit">
                            <el-input v-model="form.unit" placeholder="请输入单位"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="同步到用户模块" prop="sync">
                            <el-radio-group v-model="form.sync">
                                <el-radio label="1">是</el-radio>
                                <el-radio label="0">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="展示在监控页">
                            <el-radio-group v-model="form.showMonitor">
                                <el-radio  :label="true">是</el-radio>
                                <el-radio :label="false">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="展示在管理页">
                            <el-radio-group v-model="form.showStatus">
                                <el-radio  :label="true">是</el-radio>
                                <el-radio :label="false">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 地址导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload
                ref="upload"
                :limit="1"
                accept=".xlsx, .xls"
                :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport"
                :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress"
                :on-success="handleFileSuccess"
                :auto-upload="false"
                drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload.updateSupport"/>
                        是否更新已经存在的用户数据
                    </div>
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                             @click="importTemplate"
                    >下载模板
                    </el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {
    addFertilizerDistributorV1Address,
    delFertilizerDistributorV1Address,
    getFertilizerDistributorV1Address,
    listFertilizerDistributorV1Address,
    updateFertilizerDistributorV1Address
} from '@/api/module/fertilizerDistributorV1Address'

import { getFertilizerDistributorV1 } from '@/api/module/fertilizerDistributorV1'

import { sendMessage, sendMultipleMessage } from '@/api/netty/nettyMessage'
import { getToken } from '@/utils/auth'

export default {
    name: 'FertilizerDistributorV1Address',
    dicts: ['biz_value_type', 'biz_weight'],
    // props: {
    //     moduleId: {
    //         type: Number
    //     }
    // },
    data() {
        return {
            // 读取倒计时
            readDown: 0,
            // 写入倒计时
            writeDown: 0,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 施肥机v1地址表格数据
            fertilizerDistributorV1AddressList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                addressName: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            messageObj: {
                instruction: '10',
                paramType: '0'
            },
            timerRead: null,
            timerWrite: null,
            // 批量操作的id
            batchIdList: [],
            messageList: [],
            moduleInfo: null,
            webClientId: this.genUUID(10, 16),

            addressTypeList: [
                { value: '0', label: '正常地址' },
                { value: '1', label: '最近一个历史数据相对位置' },
                { value: '2', label: '开启通道数' },
                { value: '3', label: '历史记录' }
            ],
            channelNumList: [
                { value: '1', label: '通道1' },
                { value: '2', label: '通道2' },
                { value: '3', label: '通道3' },
                { value: '4', label: '通道4' }
            ],
            operationType: '',
            moduleId: null,
            // 地址导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: '',
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: 'Bearer ' + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + '/module/fertilizerDistributorV1Address/importData'
            }
        }
    },
    beforeUpdate() {
        this.$nextTick(() => {
            this.$refs.addressTable.doLayout()
        })
    },
    created() {
        const moduleId = this.$route.params && this.$route.params.moduleId
        this.queryParams.moduleId = moduleId
        this.moduleId = moduleId
        this.getList()
        this.getModuleInfo()
    },
    destroyed() {
        if (this.ws !== undefined) {
            this.ws.close()
            // 离开页面清空倒计时
            this.fertilizerDistributorV1AddressList.forEach(item => {
                clearInterval(item.timerRead)
                clearInterval(item.timerWrite)
            })
        }
    },
    methods: {

        getModuleInfo() {
            getFertilizerDistributorV1(this.queryParams.moduleId).then(response => {
                this.moduleInfo = response.data
            })
        },

        getList() {
            this.loading = true
            listFertilizerDistributorV1Address(this.queryParams).then(response => {
                this.fertilizerDistributorV1AddressList = response.rows
                for (let item of this.fertilizerDistributorV1AddressList) {
                    // 判断写入还是读取方式
                    item.operationType = ''
                    // 写入和读取的加载动画
                    item.readLoading = false
                    item.writeLoading = false
                    // 写入和读取的文本控制
                    item.readText = '读取'
                    item.writeText = '写入'
                    // 写入和读取的倒计时时间
                    item.readSecond = 0
                    item.writeSecond = 0
                    // 写入和读取倒计时返回值
                    item.timerRead = null
                    item.timerWrite = null
                    // 超时判断
                    item.overReadTime = true
                    item.overWriteTime = true
                }
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                addressName: '',
                hex: null,
                decimalDigit: null,
                bitSize: null,
                registerSize: 1,
                valueType: 'int',
                attribute: null,
                attributeArr: ['0'],
                isSigned: '0',
                weight: '1',
                unit: null,
                relativeAddress: null,
                showMonitor:null,
                showStatus:null,
                offsetLength: null,
                addressValue: null,
                addressType: '0',
                channelNum: '1',
                moduleId: this.moduleId,
                sync: '1',
                remark: null
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加施肥机v1地址'
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getFertilizerDistributorV1Address(id).then(response => {
                this.form = response.data
                this.form.sync = '1'
                this.open = true
                this.title = '修改施肥机v1地址'
            })
        },
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateFertilizerDistributorV1Address(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addFertilizerDistributorV1Address(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除施肥机v1地址编号为"' + ids + '"的数据项？').then(function() {
                return delFertilizerDistributorV1Address(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },
        batchRead() {
            this.messageList = []
            this.ids.forEach(id => {
                let d = this.fertilizerDistributorV1AddressList.find((item) => {
                    console.log(item)
                    return item.id === id
                })
                let index = this.fertilizerDistributorV1AddressList.indexOf(d)
                // d['readLoading'] = true
                this.$set(this.fertilizerDistributorV1AddressList, index, d)
                let obj = {
                    gatewayId: this.moduleInfo.gatewayId,
                    id,
                    webClientId: this.webClientId,
                    instruction: '10',
                    paramType: '0',
                    operationType: '0',
                    data: {
                        moduleId: this.moduleId,
                        index,
                        value: d['addressValue']
                    }
                }
                this.messageList.push(obj)
            })
            sendMultipleMessage(this.messageList).then(response => {
                console.log(6666, response)
                if (response.code === 200) {
                    // 成功后建立websocket
                    let obj = {
                        'webClientId': this.webClientId,
                        'type': 'login'
                    }
                    this.sendMessage(obj)
                }
            }, error => {
            })
            this.$refs.addressTable.clearSelection()
        },
        handleOperation(row, index, operationType) {
            this.fertilizerDistributorV1AddressList[index].operationType = operationType
            let data = this.fertilizerDistributorV1AddressList[index]
            if (operationType === '0') {
                data['readLoading'] = true
            }
            if (operationType === '1') {
                data['writeLoading'] = true
            }
            this.getTimer(data, index, operationType)
            // console.log('读取', row)
            this.$set(this.fertilizerDistributorV1AddressList, index, data)

            this.messageObj.gatewayId = this.moduleInfo.gatewayId
            this.messageObj.id = row.id
            this.messageObj.operationType = operationType
            this.messageObj.webClientId = this.webClientId
            this.messageObj.data = {
                moduleId: this.moduleId,
                index,
                value: row.addressValue
            }
            this.messageObj.writeValue = row.addressValue
            this.send2Web(index)
        },
        resetLoading(data) {
            let index = data['index']
            if (index !== undefined) {
                let value = data['value']
                let d = this.fertilizerDistributorV1AddressList[index]
                // d['readLoading'] = false
                // d['writeLoading'] = false
                d['addressValue'] = value
                this.$set(this.fertilizerDistributorV1AddressList, index, d)
            }
        },
        send2Web(index) {
            sendMessage(this.messageObj).then(response => {
                if (response.code === 200) {
                    // 成功后建立websocket
                    let obj = {
                        'webClientId': this.webClientId,
                        'type': 'login'
                    }
                    this.sendMessage(obj)
                }
            }, error => {
                let rowData = this.fertilizerDistributorV1AddressList[index]
                // this.reset()
                if (rowData.operationType === '0') {
                    this.fertilizerDistributorV1AddressList[index].overReadTime = false
                    this.fertilizerDistributorV1AddressList[index].readSecond = 0
                }
                if (rowData.operationType === '1') {
                    this.fertilizerDistributorV1AddressList[index].writeSecond = 0
                    this.fertilizerDistributorV1AddressList[index].overWriteTime = false
                }
                this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
            })
        },
        // websocket 发送消息给服务器
        sendMessage(obj) {
            if (this.ws !== undefined && this.ws.readyState === 1) {
                console.log('已存在')
                // this.$websocket.sendMsg(obj)
            } else {
                this.localSocket(obj)
            }
        },
        localSocket(obj) {
            let that = this
            if ('WebSocket' in window) {
                console.log('您的浏览器支持 WebSocket')
                // console.log('that.ws', that.ws)
                if (that.ws !== undefined && that.ws.readyState === 1) {
                    console.log('存在连接')
                    return
                }

                that.ws = new WebSocket(that.$websocket.url)
                that.$websocket.setWs(that.ws)
                that.ws.onopen = function() {
                    console.log('开始连接')
                    // that.sendMessage(obj)
                    that.$websocket.sendMsg(obj)
                }
                // websocket 接收服务器消息
                that.ws.onmessage = function(result) {
                    console.log('收到websocket回复', result)
                    that.processData(result.data)
                }
                that.ws.onclose = function() {
                    // 防链接超时，（websocket在一定时间内没有数据交互，就会断开），关闭后重启
                    console.log('连接已关闭')
                    that.ws.close()
                    // setTimeout(() => {
                    //     that.localSocket()
                    // }, 2000)
                }
            } else {
                // 浏览器不支持 WebSocket
                console.log('您的浏览器不支持 WebSocket!')
            }
        },
        // 获取超时倒计时
        getTimer(rowData, index, operationType) {
            rowData.readSecond = 30
            rowData.writeSecond = 30
            // 读取
            if ('0' === operationType) {
                rowData.timerRead = setInterval(() => {
                    if (rowData.readSecond > 0) {
                        rowData.readText = rowData.readSecond + 's'
                        this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
                        --rowData.readSecond
                    } else {
                        clearInterval(rowData.timerRead)
                        if (rowData.overReadTime) {
                            this.$message.info('读取超时')
                        }
                        rowData.readLoading = false
                        rowData.readText = '读取'
                        this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
                    }
                }, 1000)
            }
            // 写入
            if ('1' === operationType) {
                rowData.timerWrite = setInterval(() => {
                    if (rowData.writeSecond > 0) {
                        rowData.writeText = rowData.writeSecond + 's'
                        this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
                        --rowData.writeSecond
                    } else {
                        clearInterval(rowData.timerWrite)
                        rowData.writeLoading = false
                        if (rowData.overWriteTime) {
                            this.$message.info('写入超时')
                        }
                        rowData.writeText = '写入'
                        this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
                    }
                }, 1000)
            }
        },
        processData(json) {
            console.log('json', json)

            let data = {}
            try {
                data = JSON.parse(json)
            } catch (e) {
            }
            console.log('data', data)
            let webClientId = data['webClientId']
            if (webClientId === undefined) {
                return
            }

            let cause = data['cause']
            let index = data['index']
            let rowData = this.fertilizerDistributorV1AddressList[index]
            this.resetLoading(data)
            if (webClientId === this.webClientId) {
                if (cause !== undefined) {
                    this.$message.error(cause)
                    if (rowData.operationType === '0') {
                        rowData.overReadTime = false
                        rowData.readSecond = 0
                        rowData.readLoading = false
                    }
                    if (rowData.operationType === '1') {
                        rowData.writeSecond = 0
                        rowData.overWriteTime = false
                        rowData.writeLoading = false
                    }
                } else {
                    this.$message.success('成功')
                    if (rowData.operationType === '0') {
                        // console.log('读取成功')
                        rowData.overReadTime = false
                        rowData.readSecond = 0
                        rowData.readLoading = false
                        rowData.addressValue = data['value']
                    }
                    if (rowData.operationType === '1') {
                        // console.log('写入成功')
                        rowData.writeSecond = 0
                        rowData.overWriteTime = false
                        rowData.writeLoading = false
                        rowData.addressValue = data['writeValue']
                    }
                }
                this.$set(this.fertilizerDistributorV1AddressList, index, rowData)
            }
        },

        handleExport() {
            this.download('module/fertilizerDistributorV1Address/export', {
                ...this.queryParams
            }, `fertilizerDistributorV1Address_${new Date().getTime()}.xlsx`)
        },

        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = '地址导入'
            this.upload.open = true
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('module/fertilizerDistributorV1Address/importTemplate', {}, `address_template_${new Date().getTime()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false
            this.upload.isUploading = false
            this.$refs.upload.clearFiles()
            this.$alert('<div style=\'overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;\'>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
            this.getList()
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit()
        }
    }
}
</script>

<style>
</style>
